/**
 * @author liangxi
 * @since 2023/12/21
 */

#ifndef DESIGNPATTERN_WEATHERDATA_H
#define DESIGNPATTERN_WEATHERDATA_H

#include <vector>
#include "../Subject.h"

namespace Observer {

    class WeatherData : public Subject {
    public:
        WeatherData(float temp, float humidity, float pressure) : temp(temp), humidity(humidity), pressure(pressure) {};

        void registerObserver(const std::shared_ptr<Observer> &observer) override {
            observerVector.push_back(observer);
        }

        void removeObserver(const std::shared_ptr<Observer> &observer) override {
            auto target = std::find(observerVector.begin(), observerVector.end(), observer);
            if (target != observerVector.end()) {
                observerVector.erase(target);
            }
        }

        void notifyObservers() override {
            for (const auto &tmp: observerVector) {
                tmp->update(temp, humidity, pressure);
            }
        }

        void measurementsChanged() {
            notifyObservers();
        }

        void setMeasurements(float temp, float humidity, float pressure) {
            this->temp = temp;
            this->humidity = humidity;
            this->pressure = pressure;
            measurementsChanged();
        }

        float getTemp() const {
            return temp;
        }

        float getHumidity() const {
            return humidity;
        }

        float getPressure() const {
            return pressure;
        }

    private:
        float temp{0.0};
        float humidity{0.0};
        float pressure{0.0};
        std::vector<std::shared_ptr<Observer>> observerVector;
    };

} // Observer

#endif //DESIGNPATTERN_WEATHERDATA_H
